﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Model;

namespace LaKuaShop_TestDemo.Page
{
    /// <summary>
    /// ShopCartPage.xaml 的交互逻辑
    /// </summary>
    public partial class ShopCartPage : System.Windows.Controls.Page
    {
        public double sum;
        public ShopCartPage()
        {
            InitializeComponent();
            InitList();
        }

        //初始化列表
        public void InitList()
        {
            List<Good> ex = new List<Good>();
            Sum allsum = new Sum();
            sum = 0;
            BLL.BuyCart buyCart = new BLL.BuyCart();
            BLL.Product Pro = new BLL.Product();
            BuyCartInfo cartinfo = buyCart.GetBuyCart(Common.User.account);
            for (int i = 0; i < cartinfo.PidList.Count; i++)
            {
                ProductInfo productInfo = Pro.ShowProductInfo(cartinfo.PidList[i]);
                byte[] img = Pro.ShowProductImg(cartinfo.PidList[i]);
                BitmapImage bmp = new BitmapImage();
                if (img == null)
                {
                    bmp = new BitmapImage(new Uri("/newlogo.png", UriKind.Relative));
                }
                else
                {
                    MemoryStream stream = new MemoryStream(img);
                    bmp.BeginInit();//初始化
                    bmp.StreamSource = stream;//设置源
                    bmp.EndInit();//初始化结束
                }
                string Singlestr  = "￥" + string.Format("{0:n2}", productInfo.Pprice);
                ex.Add(new Good(bmp, Singlestr, productInfo.Pname, cartinfo.Bnum[i]));
                sum += productInfo.Pprice * cartinfo.Bnum[i];
            }
            string str = string.Format("{0:N2}", sum);
            allsum.Sumprice = str;
            sumtext.DataContext = allsum;
            ShopCartlist.ItemsSource = ex;
        }
        //结算订单
        private void Sum_ButtonClick(object sender, RoutedEventArgs e)
        {
            BLL.Product Pro = new BLL.Product();
            BLL.BuyCart buyCart = new BLL.BuyCart();
            BuyCartInfo cartinfo = buyCart.GetBuyCart(Common.User.account);
            foreach (Good item in ShopCartlist.Items)
            {
                List<ProductInfo> prolist = Pro.SearchProduct(item.name);
                ProductInfo pro = prolist[0];
                if (pro.Pnum < item.num)
                {
                    MessageBox.Show("库存不足~请耐心等待管理员补货哦♥");
                    return;
                }
            }
            NavigationService.GetNavigationService(this).Navigate(new OrderPage(cartinfo));
        }

        //再去逛逛
        private void Navi_button(object sender, RoutedEventArgs e)
        {
            NavigationService.GetNavigationService(this).Navigate(new ProductShowPage());
        }
        //清空购物车
        private void Dele_button(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("确认清空购物车?", "确认提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                BLL.Product Pro = new BLL.Product();
                BLL.BuyCart buyCart = new BLL.BuyCart();
                foreach (Good item in ShopCartlist.Items)
                {
                    List<ProductInfo> prolist = Pro.SearchProduct(item.name);
                    ProductInfo pro = prolist[0];
                    buyCart.DelOneBuyCart(Common.User.account, pro.Pid);
                }
                InitList();
            }
        }
        //删除单个商品
        private void Del_button(object sender, RoutedEventArgs e)
        {
            var mySelectedElement = (Good)ShopCartlist.SelectedItem;
            string selectedID = mySelectedElement.name;
            BLL.Product Pro = new BLL.Product();
            BLL.BuyCart buyCart = new BLL.BuyCart();
            List<ProductInfo> prolist = Pro.SearchProduct(selectedID);
            selectedID = prolist[0].Pid;
            buyCart.DelOneBuyCart(Common.User.account, selectedID);
            InitList();
        }

        private void sub_button(object sender, RoutedEventArgs e)
        {
            var mySelectedElement = (Good)ShopCartlist.SelectedItem;
            string selectedID = mySelectedElement.name;
            BLL.Product Pro = new BLL.Product();
            BLL.BuyCart buyCart = new BLL.BuyCart();
            List<ProductInfo> prolist = Pro.SearchProduct(selectedID);
            selectedID = prolist[0].Pid;
            buyCart.AddOneBuyCart(Common.User.account, selectedID, -1);
            InitList();
        }

        private void add_button(object sender, RoutedEventArgs e)
        {
            var mySelectedElement = (Good)ShopCartlist.SelectedItem;
            string selectedID = mySelectedElement.name;
            BLL.Product Pro = new BLL.Product();
            BLL.BuyCart buyCart = new BLL.BuyCart();
            List<ProductInfo> prolist = Pro.SearchProduct(selectedID);
            selectedID = prolist[0].Pid;
            buyCart.AddOneBuyCart(Common.User.account, selectedID, 1);
            InitList();
        }
    }
    class Good
    {
        public BitmapImage pic { get; set; }
        public string price { get; set; }
        public string name { get; set; }
        public int num { get; set; }
        public Good(BitmapImage pic, string price, string name, int num)
        {
            this.pic = pic;
            this.price = price;
            this.name = name;
            this.num = num;
        }
    }

    public class Sum
    {
        public static string sumprice;

        public string Sumprice { get => sumprice; set => sumprice = value; }
    }
}
